options validvarname=upcase;
%let domain=pp;

/*************************************************************************************************/
*** get source data ;
%get_data(pre=sdtm,lib=sdtm,supp=y,lst=dm se tv ta &domain);
%get_data(pre=adam,lib=adam,lst=adsl);

%load_tdd_or_not;

*** gather info ;
 ** make param stuff ;
%adam_param(dom=&domain); * default input dataset => pre=sdtm in=&domain from %get_data ;

*** code block
===> add derived stuff that does not require avisit/aperiod/time point, treatment, analysis date/time, values, baseline
;

*** create avisit/n, atpt/n, aperiod/c, and aphase/n ;
%let no_window=1;
%macro test_if;
%if &no_window %then %do;
   %adam_no_window_awhilo(fu_vnum=&veos);
%end;
%else %do;
   %adam_no_window_awhilo(fu_vnum=&veos);
%end;
%mend test_if;
%test_if;

%include "\\algopharm.com\algorithmepharmadata\Departments\Biostatistics\Standard Folder Structure\Macro Repository\test\adam_timing_001.sas";
%adam_timing(dom=&domain,aphase=1); * default input dataset => in=amp_fin from %adam_mk_param and hilo=anwa_fin from %adam_no_window_awhilo ;

*** code block
===> add derived stuff that does not require treatment, analysis date/time, values, baseline
;

*** get the treatments for trtp/a ;
%adam_trt(dom=&domain,bydat=1,dat=pprftdtc,eq_sign=1,debug=1); * default input dataset => in=atm_fin from %adam_timing ;


data test;
  set adsl_trt;

  date = %mk_dtc(dat=tredt,wtim=1);

run;



*** code block
===> add derived stuff that does not require analysis date/time, values, baseline
;

*** make the analysis date/time ;
%adam_adt(dom=&domain,target=1); * default input dataset => in=atr_fin from %adam_trt ;

*** code block
===> add derived stuff that does not require values, baseline
;

*** make the analysis values ;
%adam_aval(dom=&domain); * default input dataset => in=adt_fin from %adam_adt ;

*** code block
===> add derived stuff that does not require baseline
;

*** create analysis records flag ;
%*adam_anl01fl(in=avl_fin); * default input dataset => in=abv_fin from %adam_bsl_val ;
%adam_anl01fl(in=avl_fin,by=usubjid param avisitn cond awtdiff ady /*pcseq*/,first=cond); * default input dataset => in=abv_fin from %adam_bsl_val ;


*** get decimal precision for each param ;
%get_deci(dom=&domain,lib=sdtm,in=&domain);
proc sql;
create table toder as select a.*,ifn(missing(b.deci),0,b.deci) as nbprec
   from a01_fin as a left join gd_deci as b on a.&domain.testcd=b.&domain.testcd;
quit;

*** derive adam stuff ;
data todef;
   set toder; 
   length param2cd $8. param2 $200.;

   *** derive other stuff here as necessary ;
   param = tranwrd(param,"| Nicotine | Plasma"," ");

    ** adjust param/cd for pk group ;
   param2n=paramn;
   param2cd=strip(paramcd);

   if lowcase(paramcd)="aucifo"       then param2="%upcase(auc)0-inf";
   else if lowcase(paramcd)="auclst"  then param2="%upcase(auc)0-T";
   else if lowcase(paramcd)="auctinf" then param2="%upcase(auc)0-T/inf";
   else if lowcase(paramcd)="cmax"    then param2=propcase("cmax");
   else if lowcase(paramcd)="lamz"    then param2="Lambda Z";
   else if lowcase(paramcd)="lamzhl"  then param2="Thalf";
   else if lowcase(paramcd)="lamzll"  then param2="Tlin";
   else if lowcase(paramcd)="lamznpt" then param2="Number of Points";
   else if lowcase(paramcd)="lamzul"  then param2="Lambda Z Upper Limit";
   else if lowcase(paramcd)="r2"      then param2="R2";
   else if lowcase(paramcd)="tlst"    then param2="T1/2";
   else if lowcase(paramcd)="tmax"    then param2=propcase("tmax");
   else if lowcase(paramcd)="aucpeo"  then param2="% extrapolated";
   else param2=strip(param);

    ** log e value ;
   if 0<aval then leaval=log(aval);

run;

*** define listing variables ;
proc sort data=todef out=tolbl;
by usubjid;
   *** listing definitions ;
run;

*** any variable not normally in the adam ig should be given a label here ;
data toflg;
   set  tolbl;
   label
      param2   = "Parameter 2"
      param2n  = "Parameter 2 (N)"
      param2cd = "Parameter 2 Code"
      leaval   = "Log e of Imputed Analysis Value"
      nbprec   = "Decimal Precision"
   ;

run;

*** table flags ;
%let trt=trtp;
/* flabel=1234567890123456789012345678901234567890*/
 ** flags ;
/* ===> uncomment to create flags if necessary for tables
%mk_flag(in=toflg,flag=aocc01fl,
   whr=dummy_condition,by=&trt usubjid adt lbseq,first=usubjid,
   flabel=dummy_label);
*/

data final; 
  set toflg; 

  select(avisitn);
    when(2) trtp = strip(trt01a);
    when(9) trtp = strip(trt02a);
   otherwise;
  end;

  trta = strip(trtp);
  trtan = trtpn;

  drop trt02p trt01pn trt02pn trt02a trt01an trt02an 
       age ethnic race rfendtc
       rfstdtc sex tr01edt tr01edtm tr01sdt
       tr01sdtm tr02edt tr02edtm tr02sdt tr02sdtm trt01a
       trt01p trtedt trtedtm trtsdt trtsdtm aperiod aperiodc
       aphase aphasen aw: tr03: tr04: tr05: tr06:
       trt03: trt04: trt05: trt06: 
       ;


run;

** Assign ASEQ;
proc sort data=final;
  by studyid usubjid param paramcd avisitn avisit;
run;

data final;
   merge  final(in=ppp) adam.adsl(keep=usubjid arm armcd actarm actarmcd);
   by usubjid; 
if ppp;
  retain aseq 0;

  if pptest = "R squared" then pptest = "R Squared";

  if first.usubjid then aseq = 1;
  else aseq + 1;

  label aseq     = "Analysis Sequence Number"
        AVISIT   = "Analysis Visit"
        AVISITN  = "Analysis Visit (N)"
        COND     = "Condition"
        USE      = "Use session"
        ;
*to fix trtp for withdr subj;
if trtan<avisitn and armcd ne actarmcd then do; 
v=index(armcd,"/");
trtp=strip(substr(armcd,v+1,1));
trta=strip(substr(actarmcd,v+1,1));
if trta="A" then do; trtan=4; end; if trta="B" then do; trtan=5; end; if trta="C" then do; trtan=6; end;
if trtp="A" then do; trtpn=4; end; if trtp="B" then do; trtpn=5; end; if trtp="C" then do; trtpn=6; end;
end;
run;

*** make adam ;
%make_adam(in=final,dom=&domain,key=studyid usubjid param paramcd %vchk(final,avisit) use,add=ppspec);

** Reorder the variables per Justine;
 proc sql; 
  create table adamw.adpp (label="Pharmacokinetic Parameters") as
  select STUDYID,USUBJID,SUBJID,SITEID,ARM, ARMCD, ACTARM, ACTARMCD,ASEQ,TRTP,TRTPN,TRTA,TRTAN,
         ENRLFL,RANDAFL,RANDBFL,SAFFL,PKFL,PDFL,COMPLFL,ADY,AVISIT,
         AVISITN,PARCAT1N,PARCAT1,PARAMN,PARAMCD,PARAM,PARAM2N,PARAM2CD,
         PARAM2,AVAL,AVALC,ANL01FL,COND,USE,LEAVAL,NBPREC,PKPOP,PPSPEC      
    from adam.adpp
    order by studyid,usubjid,paramn;
 quit;

*** clear work and other libnames ;
%clrw;


